﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.IO;
using System.Net;
using mshtml;


namespace OneMangaReader
{
    // Facade Class to build the Manga
    class ManagaBuilder
    {
        private Manga manga;

        public void CreateManga(string manga_id, string url)
        {
            manga = new Manga(manga_id, url);
            manga.MangaPage = retrieveHTML(manga.Url);
            manga.Chapters = extractChapters(manga.MangaPage);

        }

        // Retrieve the HTML from the Manga URL
        private string retrieveHTML(string url)
        {
            // WebClient object
            WebClient client = new WebClient();

            // Retrieve resource as a stream
            Stream data = client.OpenRead(new Uri(url));

            // Retrive the text
            StreamReader reader = new StreamReader(data);
            string htmlContent = reader.ReadToEnd();

            // Cleanup
            data.Close();
            reader.Close();

            return htmlContent;
        }

        // Extract Chapters
        private Chapter[] extractChapters(string htmlContent)
        {
            // Obtain the document interface
            IHTMLDocument2 htmlDocument = (IHTMLDocument2)new mshtml.HTMLDocument();
            htmlDocument.write(htmlContent);

            IHTMLDocument2 tableContents = (IHTMLDocument2)new mshtml.HTMLDocument();

            ArrayList chapterList = new ArrayList();

            foreach (IHTMLElement el in 
                (IHTMLElementCollection)htmlDocument.body.all)
            {
                if (el.tagName.Equals("TABLE") && 
                    el.className.Equals("ch-table"))
                {
                    foreach (IHTMLElement cElement in 
                        (IHTMLElementCollection)tableContents.body.all)
                    {
                        if (cElement.tagName.Equals("A"))
                        {
                            chapterList.Add(
                                new Chapter(cElement.innerText, 
                                    ((IHTMLAnchorElement)cElement).pathname));
                        }
                    }
                }
            }

            return (Chapter[])chapterList.ToArray();
            

                //    Dim doc As IHTMLDocument2
                //doc = New HTMLDocument
                //doc.write(htmlContent)


                //Dim tblContents As IHTMLDocument2
                //tblContents = New HTMLDocument

                //For Each el As IHTMLElement In CType(doc.body.all, IHTMLElementCollection)
                //    If el.tagName = "TABLE" And el.className = "ch-table" Then

                //        tblContents.write(el.outerHTML)

                //        For Each cElement As IHTMLElement In CType(tblContents.body.all, IHTMLElementCollection)
                //            If cElement.tagName = "A" Then

                //                Me.cboChapters.Items.Add(New ComboPair(cElement.innerText, CType(cElement, IHTMLAnchorElement).pathname))

                //            End If
                //        Next

                //    End If

                //Next
        }

    }
}
